package com.feioou.blogdemo.controller;

import com.feioou.blogdemo.entity.User;
import com.feioou.blogdemo.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @description: 用户控制器
 * @author: chenyingjie
 * @create: 2019-11-26 14:47
 **/
@RestController
@RequestMapping(path = "/user")
public class UserController {

    @Autowired
    private UserRepository userRepository;

    @PostMapping(path = "/add")
    public String add(@RequestBody User user) {
        if (user.getName().isEmpty() || user.getPassword().isEmpty()) {
            return "fail";
        }
        userRepository.save(user);
        return "success";
    }

    @GetMapping(path = "/list")
    public List<User> list() {
        return (List<User>) userRepository.findAll();
    }
}
